package com.emip.servlet.baseinfo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.emip.bean.ItemBean;
import com.emip.bean.MonitorTypeBean;
import com.emip.config.SysParams;
import com.emip.pojo.Table;
import com.emip.service.ItemService;
import com.emip.service.MonitorTypeService;
import com.emip.util.TableFormat;



@Controller
@RequestMapping(value="/base/item")
public class ItemAction {
	@Autowired
	ItemService itemService;
	
	@Autowired
	MonitorTypeService monitorTypeService;
	
	@RequestMapping(value="/management")
	public String index() {		
		return "/baseInfo/ItemManagement";
	}
	
	@RequestMapping(value="/getAllItems")
	public @ResponseBody Table<ItemBean> getAllItems() {
		List<ItemBean> result = itemService.getAll();
		int records = result.size();
		int totalPages = records / SysParams.gridDefinedRowNumber + 1;
		return new TableFormat<ItemBean>().formTable(totalPages, 1, records, result);
	}
	
	@RequestMapping(value="/getItemByID",method=RequestMethod.POST, headers="Accept=application/json")
	public @ResponseBody ItemBean getItemByID(@RequestParam("eid") String eid) {
		return itemService.getItemByID(eid);
	}
	
	@RequestMapping(value="/addItem",method=RequestMethod.POST)
	public @ResponseBody ItemBean addItem(@RequestParam("id") String id, @RequestParam("MTypeID")  int MTypeID, 
			@RequestParam("name") String name, @RequestParam("abrName") String abrName, @RequestParam("nationalName") String nationalName, @RequestParam("eMaxValue") double eMaxValue,
			@RequestParam("eMinValue") double eMinValue, @RequestParam("overMaxValue") float overMaxValue, @RequestParam("overMinValue") double overMinValue,
			@RequestParam("standUnit") String standUnit, @RequestParam("scale") int scale, @RequestParam("maxRange") double maxRange, @RequestParam("minRange") double minRange) {
		MonitorTypeBean mBean = monitorTypeService.getMonitorTypeByID(MTypeID);
		return itemService.addItem(id, mBean, name, abrName, nationalName, eMaxValue, eMinValue, overMaxValue, overMinValue, standUnit, scale, maxRange, minRange);			
	}
	
	
	
	@RequestMapping(value="/updateItem",method=RequestMethod.POST)
	public @ResponseBody ItemBean updateItem(@RequestParam("id") String id, @RequestParam("MTypeID")  int MTypeID, 
			@RequestParam("name") String name, @RequestParam("abrName") String abrName, @RequestParam("nationalName") String nationalName, @RequestParam("eMaxValue") float eMaxValue,
			@RequestParam("eMinValue") double eMinValue, @RequestParam("overMaxValue") double overMaxValue, @RequestParam("overMinValue") double overMinValue,
			@RequestParam("standUnit") String standUnit, @RequestParam("scale") int scale, @RequestParam("maxRange") double maxRange, @RequestParam("minRange") double minRange) {
		MonitorTypeBean mBean = monitorTypeService.getMonitorTypeByID(MTypeID);
		return itemService.updateItem(id, mBean, name, abrName, nationalName, eMaxValue, eMinValue, overMaxValue, overMinValue, standUnit, scale, maxRange, minRange);
	}
	
	@RequestMapping(value="/deleteItem",method=RequestMethod.POST)
	public @ResponseBody String deleteItem(@RequestParam("eID") String id) {
		itemService.deleteItem(id);
		return "success";
	}
	
	@RequestMapping(value="/getItemsByMonitorTypeID" ,method=RequestMethod.POST)
	public @ResponseBody List<ItemBean> getItemsByMonitorTypeID(@RequestParam("mTypeID") int mTypeID) {
		MonitorTypeBean mTypeBean = monitorTypeService.getMonitorTypeByID(mTypeID);
		return itemService.getItemsByMonitorType(mTypeBean);
	}
}
